package network;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient1 {
    Socket socket = null;
    TcpEchoClient1(int serverPort, String serverIp) throws IOException {
        socket = new Socket(serverIp, serverPort);
    }
    public void start() {
        System.out.printf("[%s:%d] 客户端上线\n",socket.getInetAddress(), socket.getPort());
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()) {
            Scanner scanner = new Scanner(System.in);
            Scanner scanner1 = new Scanner(inputStream);
            PrintWriter printWriter = new PrintWriter(outputStream);
            while (true) {
                String request = scanner.next();
                printWriter.println(request);
                printWriter.flush();
                String response = scanner1.next();
                System.out.println(response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient1 client1 = new TcpEchoClient1(1314, "127.0.0.1");
        client1.start();
    }
}
